<template>
  <div class="layui-container fly-marginTop">
    <div class="fly-panel fly-panel-user" pad20>
      <div class="layui-tab layui-tab-brief" lay-filter="user">
        <ul class="layui-tab-title">
          <li>
            <router-link :to="{name: 'login'}">登入</router-link>
          </li>
          <li class="layui-this">注册</li>
        </ul>
        <div class="layui-form layui-tab-content" style="padding: 20px 0;">
          <div class="layui-tab-item layui-show">
            <div class="layui-form layui-form-pane">
              <validation-observer ref="observer" tag="form" @submit.prevent="submit()">
                <div class="layui-form-item">
                  <label for="L_username" class="layui-form-label">用户名</label>
                  <div class="layui-input-inline">
                    <validation-provider
                      rules="required|email"
                      name="username"
                      v-slot="{errors}"
                      vid="username"
                    >
                      <input
                        id="L_username"
                        type="text"
                        placeholder="请输入用户名"
                        name="username"
                        v-model="username"
                        autocomplete="off"
                        class="layui-input"
                      />
                      <span class="error">{{errors[0]}}</span>
                    </validation-provider>
                  </div>
                  <div class="layui-form-mid layui-word-aux">将会成为您唯一的登入名</div>
                </div>
                <div class="layui-form-item">
                  <label for="L_nickname" class="layui-form-label">昵称</label>
                  <div class="layui-input-inline">
                    <validation-provider
                      rules="required|min:4"
                      name="nickname"
                      v-slot="{errors}"
                      vid="nickname"
                    >
                      <input
                        id="L_nickname"
                        type="text"
                        name="nickname"
                        placeholder="请输入您的昵称"
                        autocomplete="off"
                        class="layui-input"
                        v-model="nickname"
                      />
                      <span class="error">{{errors[0]}}</span>
                    </validation-provider>
                  </div>
                </div>
                <div class="layui-form-item">
                  <label for="L_pass" class="layui-form-label">密码</label>
                  <div class="layui-input-inline">
                    <validation-provider
                      :rules="{required: true, min: 6, max: 16}"
                      v-slot="{ errors }"
                      name="password"
                      vid="password"
                    >
                      <input
                        id="L_pass"
                        type="password"
                        name="pass"
                        placeholder="请输入密码"
                        autocomplete="off"
                        class="layui-input"
                        v-model="password"
                      />
                      <span class="error">{{errors[0]}}</span>
                    </validation-provider>
                  </div>
                  <div class="layui-form-mid layui-word-aux">6到16个字符</div>
                </div>
                <div class="layui-form-item">
                  <label for="L_repass" class="layui-form-label">确认密码</label>
                  <div class="layui-input-inline">
                    <validation-provider
                      :rules="{required: true, is: this.password}"
                      name="password"
                      v-slot="{errors}"
                      vid="repassword"
                    >
                      <input
                        id="L_repass"
                        type="password"
                        name="repass"
                        placeholder="请再次输入密码"
                        autocomplete="off"
                        class="layui-input"
                        v-model="repassword"
                      />
                      <span class="error">{{errors[0]}}</span>
                    </validation-provider>
                  </div>
                </div>
                <div class="layui-form-item">
                  <label for="L_vercode" class="layui-form-label">验证码</label>
                  <div class="layui-input-inline">
                    <validation-provider
                      rules="required|length:4"
                      name="code"
                      v-slot="{ errors }"
                      vid="code"
                    >
                      <input
                        id="L_vercode"
                        type="text"
                        name="code"
                        placeholder="请输入验证码"
                        autocomplete="off"
                        class="layui-input"
                        v-model="code"
                      />
                      <span class="error">{{errors[0]}}</span>
                    </validation-provider>
                  </div>
                  <div class="layui-form-mid">
                    <span v-html="svg" class="code" @click="_getCaptcha"></span>
                  </div>
                </div>
                <div class="layui-form-item">
                  <button class="layui-btn">立即注册</button>
                </div>
              </validation-observer>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ValidationProvider, ValidationObserver } from 'vee-validate'
import { getCaptcha, reg } from '@/api/login'
export default {
  name: 'reg',
  data() {
    return {
      username: '',
      code: '',
      password: '',
      nickname: '',
      repassword: '',
      svg: ''
    }
  },
  components: {
    ValidationObserver,
    ValidationProvider
  },
  mounted() {
    this._getCaptcha()
  },
  methods: {
    async _getCaptcha() {
      let sid = this.$store.state.sid
      const res = await getCaptcha(sid)
      if (res.code === 200) {
        this.svg = res.data
      }
    },
    async submit() {
      const invalid = await this.$refs.observer.validate()
      if (!invalid) {
        return
      }
      const res = await reg({
        username: this.username,
        password: this.password,
        name: this.nickname,
        code: this.code,
        sid: this.$store.state.sid
      })
      switch (res.code) {
        case 200:
          this.username = ''
          this.password = ''
          this.repassword = ''
          this.name = ''
          this.code = ''
          requestAnimationFrame(() => {
            this.$refs.observer.reset()
          })
          this.$alert('注册成功')
          setTimeout(() => {
            this.$router.push('/login')
          }, 1000)
          break
        case 10001:
          this.$alert(res.message)
          break
        case 10014:
          this.$alert(res.message)
          break
        case 10016:
          this.$alert(res.message)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.error {
  color: red;
}
.code {
  color: #c00;
  position: relative;
  top: -14px;
}
</style>